package com.test;

import java.time.Duration;
import java.time.temporal.ChronoUnit;

import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.SetArgs;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.reactive.RedisReactiveCommands;

public class SetAndGetDemo {

	private Logger log = LoggerFactory.getLogger(SetAndGetDemo.class);
	private static StatefulRedisConnection<String, String> CONNECTION;
	private static RedisClient CLIENT;
	private static RedisReactiveCommands<String, String> REACTIVE_COMMAND;

	@BeforeClass
	public static void beforeClass() {
	    RedisURI redisUri = RedisURI.builder()
	            .withSentinel("10.16.34.125", 6406)
	            .withSentinelMasterId("sentinel-10.16.34.15-6381")
	            .withTimeout(Duration.of(10, ChronoUnit.SECONDS))
	            .build();
	    CLIENT = RedisClient.create(redisUri);
	    CONNECTION = CLIENT.connect();
	    REACTIVE_COMMAND = CONNECTION.reactive();
	}

	@Test
	public void testReactiveSetAndGet() throws Exception {
	    SetArgs setArgs = SetArgs.Builder.nx().ex(60000);
	    REACTIVE_COMMAND.set("name", "测试", setArgs).block();
	    REACTIVE_COMMAND.get("name").subscribe(value -> {
	    	log.info("Get命令返回:{}", value);
	    });
	    // REACTIVE_COMMAND.set("name", "测试", setArgs).then(REACTIVE_COMMAND.get("name")).subscribe(value -> log.info("Get命令返回:{}", value));
	    Thread.sleep(1000);
	}

}
